import { defineStore } from 'pinia'
import { ref } from 'vue'
import { login as loginApi, type ILogin } from '@/api/userApi'

export const useUserStore = defineStore('user', () => {
  const userInfo = ref({
    account: '123',
    password: '123'
  })

  async function login(data: ILogin) {
    const result = await loginApi(data)
    if (result.code === 2000) {
      localStorage.setItem('user', JSON.stringify(data))
      userInfo.value = {
        ...data
      }
    }
    return result
  }

  async function logout() {
    localStorage.removeItem('user')
  }

  async function setInfo(data: ILogin) {
    userInfo.value = {
      ...data
    }
  }

  return { login, logout, setInfo, userInfo }
})
